package com.example.modelDTO.converter;

import com.example.dao.RestaurantMapper;
import com.example.model.OrderUser;
import com.example.model.Restaurant;
import com.example.modelDTO.OrderUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.format.DateTimeFormatter;

public class OrderUserConverter {


    public static OrderUserDTO toDTO(OrderUser orderUser,RestaurantMapper restaurantMapper){
        OrderUserDTO dto=new OrderUserDTO();
        dto.setIfDinein(orderUser.getIfDinein());
        Restaurant restaurant=restaurantMapper.selectById(orderUser.getRestaurantId());
        String imageStr=restaurant.getRestaurantImage();
        imageStr=imageStr.substring(imageStr.lastIndexOf("/")+1);
        imageStr="../../images/restaurants/"+imageStr+".jpg";
        System.out.println(imageStr);
        dto.setOrderUserId(orderUser.getOrderId());
        dto.setRestaurantImage(imageStr);
        dto.setRestaurantName(restaurant.getRestaurantName());
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        dto.setOrderTimeStr(orderUser.getOrderTime().format(dateTimeFormatter));
        dto.setAmountDue(orderUser.getAmountDue());
        dto.setAmountPaid(orderUser.getAmountPaid());
        /*dto.setRestaurantId(orderUser.getRestaurantId());
        dto.setOrderTime(orderUser.getOrderTime());
        dto.setAmountDue(orderUser.getAmountDue());
        dto.setAmountPaid(orderUser.getAmountPaid());
        dto.setIfRounderOff(Boolean.TRUE.equals(orderUser.getIsRoundOff())? orderUser.getIsRoundOff():null);
        dto.setIfDiscounted(Boolean.TRUE.equals(orderUser.getIsDiscounted())?orderUser.getIsDiscounted():null);
        if(Boolean.TRUE.equals(dto.getIfRounderOff())){
            //抹零
            BigDecimal roundAmount=dto.getAmountDue().subtract(dto.getAmountPaid());
            dto.setRoundAmount(roundAmount);
            dto.setDisCountRate(null);
        }else{
            //打折
            BigDecimal discountRate=dto.getAmountPaid().divide(dto.getAmountDue(),4, RoundingMode.HALF_UP);
            dto.setDisCountRate(discountRate);
            dto.setRoundAmount(null);
        }
        dto.setIfDinein(orderUser.getIfDinein());*/
        return dto;

    }
}
